package com.huatai.exchange.bootup.controller;

import com.faneui.bootup.controller.AbstractRestController;
import com.faneui.common.bean.BaseResp;
import com.faneui.common.bean.IdReq;
import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.BeanMapper;
import com.faneui.common.utils.ListUtils;
import com.huatai.exchange.dal.model.ExcFileLoadRecord;
import com.huatai.exchange.facade.FileLoadRecordController;
import com.huatai.exchange.facade.bean.FileLoadRecordVo;
import com.huatai.exchange.facade.bean.QueryFileLoadRecordVoListResp;
import com.huatai.exchange.service.ClearHisDataService;
import com.huatai.exchange.service.ExcFileLoadRecordService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "loadrecord")
@Api("公共文件备份中心")
public class FileLoadRecordControllerImpl extends AbstractRestController implements FileLoadRecordController {

    private static final Logger logger = LoggerFactory.getLogger(FileLoadRecordController.class);

    @Autowired
    private ExcFileLoadRecordService excFileLoadRecordService;

    @Autowired
    private ClearHisDataService clearHisDataService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public QueryFileLoadRecordVoListResp queryFileLoadRecordList(@ModelAttribute FileLoadRecordVo vo) throws BusinessException {
        QueryFileLoadRecordVoListResp resp = new QueryFileLoadRecordVoListResp();
        try {
            ExcFileLoadRecord param = BeanMapper.map(vo, ExcFileLoadRecord.class);
            List<ExcFileLoadRecord> records = excFileLoadRecordService.queryExcFileLoadRecordList(param);
            if (ListUtils.isNotEmpty(records)) {
                List<FileLoadRecordVo> fileLoadRecordVos = BeanMapper.mapList(records, FileLoadRecordVo.class);
                resp.setFileLoadRecordVos(fileLoadRecordVos);
            }
        } catch (Exception ex) {
            logger.error("", ex);
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public BaseResp removeFileLoadRecord(@RequestBody IdReq req) {
        BaseResp resp = new BaseResp();
        try{
            clearHisDataService.clearHisDataRecord(req.getId());
        } catch (Exception ex) {
            logger.error("", ex);
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }
}
